package com.dropTables.TakingNote;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class ReminderDbHelper
{
	
	public static final String KEY_TITLE = "title";
	public static final String KEY_BODY = "body";
	public static final String KEY_ROWID = "_id";
	public static final String KEY_R_ID = "reminder_id";
	public static final String KEY_TIME = "time";
	public static final String KEY_G_LOC = "gen_loc";
	public static final String KEY_S_LOC = "spec_loc";
	
	private static final String TAG = "ReminderDbHelper";
	private DatabaseHelper mDbHelper;
	private static SQLiteDatabase mDb;
	
	private static final String DATABASE_CREATE = "create table reminders (_id integer primary key autoincrement, title text not null, body text not null, reminder_id long not null,time timestamp not null,gen_loc  string not null,spec_loc  string not null);";
	
	private static final String DATABASE_NAME = "data";
	private static final String DATABASE_TABLE = "reminders";
	private static final int DATABASE_VERSION = 1;
	
	private final Context mCtx;

	private static class DatabaseHelper extends SQLiteOpenHelper
	{
		DatabaseHelper(Context context) 
		{
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}
		@Override
		public void onCreate(SQLiteDatabase db) 
		{
			db.execSQL(DATABASE_CREATE);
		}
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
		{
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS reminders");
			onCreate(db);
		}
	}
	
	public ReminderDbHelper(Context ctx) 
	{
		this.mCtx = ctx;
	}
	public ReminderDbHelper open() throws SQLException 
	{
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}
	public void close() 
	{
		mDbHelper.close();
	}

	public static long createReminder(String title, String body, long reminder_id,long time,String gLoc,String sloc) 
	{
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_TITLE, title);
		initialValues.put(KEY_BODY, body);
		initialValues.put(KEY_R_ID, reminder_id);
		initialValues.put(KEY_TIME, time);
		initialValues.put(KEY_G_LOC, gLoc);
		initialValues.put(KEY_S_LOC, sloc);
		return mDb.insert(DATABASE_TABLE, null, initialValues);
	}
	public boolean deleteReminder(long rowId) 
	{
		return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}
	public static void clearTable()
	{
		mDb.execSQL("DELETE from "+DATABASE_TABLE);	
	}
	public Cursor fetchAllReminders() 
	{
		return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,KEY_BODY,KEY_R_ID,KEY_TIME, KEY_G_LOC, KEY_S_LOC}, null, null, null, null, null);
	}
	public Cursor fetchReminder(long rowId) throws SQLException 
	{
		Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,KEY_TITLE, KEY_BODY,KEY_R_ID,KEY_TIME,KEY_G_LOC,KEY_S_LOC}, KEY_ROWID + "=" + rowId, null,null, null, null, null);
		if (mCursor != null) 
		{
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	public boolean updateReminder(long rowId, String title, String body, long reminder_id,long time,String gLoc,String sloc) 
	{
		ContentValues args = new ContentValues();
		args.put(KEY_TITLE, title);
		args.put(KEY_BODY, body);
		args.put(KEY_G_LOC,gLoc);
		args.put(KEY_S_LOC,sloc);
		args.put(KEY_TIME,time);
		args.put(KEY_R_ID,reminder_id);
		return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
	}
	
}
